package com.dawnClass.service.impl;

import com.dawnClass.VO.CollectionCourseVO;
import com.dawnClass.mapper.CourseCollectionMapper;
import com.dawnClass.mapper.CourseMapper;
import com.dawnClass.pojo.*;
import com.dawnClass.service.AllFollowsService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AllFollowsServiceImpl implements AllFollowsService {

    @Autowired
    private CourseCollectionMapper courseCollectionMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public ServiceResponse getById(Integer consumerId) {
        //获得用户收藏数据
        CourseCollectionExample courseCollectionExample = new CourseCollectionExample();
        courseCollectionExample.setOrderByClause("collection_time desc");
        //查询该用户收藏状态为1的课程
        courseCollectionExample.createCriteria().andConsumerIdEqualTo(consumerId).andStateEqualTo(1);
        //将通过courseId查询到的课程信息封装到List中
        List<CollectionCourseVO> vos = new ArrayList<>();
        List<CourseCollection> courseCollectionList = courseCollectionMapper.selectByExample(courseCollectionExample);
        for (CourseCollection courseCollection : courseCollectionList){
            Course course = courseMapper.selectByPrimaryKey(courseCollection.getCourseId());
//            将通过courseId查询到的课程信息封装到List中
            vos.add(new CollectionCourseVO(course,courseCollection));

        }

       if(vos.size()>0&&vos!=null)
           return ServiceResponse.getSuccess(vos);
        return ServiceResponse.getFail("暂未收藏任何课程");
    }

    @Override
    public ServiceResponse updateaById(Integer collentionId ) {
        CourseCollection courseCollection = courseCollectionMapper.selectByPrimaryKey(collentionId);
        courseCollection.setState(0);
        int rows = courseCollectionMapper.updateByPrimaryKey(courseCollection);
        if(rows>0){
            return ServiceResponse.updataSuccess(rows);
        }else
        return ServiceResponse.updataFail(null);
    }
}
